package cn.ljx.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.ljx.entity.JsonReturn;
import cn.ljx.entity.TBUser;
import cn.ljx.service.TBUserService;
import cn.ljx.util.BaseUtils;

@Controller
@RequestMapping("/User")
public class UserController {
	@Autowired
    private TBUserService tbUserService;
	
	@SuppressWarnings("finally")
	@ResponseBody
	@RequestMapping(value="/getUserList",method=RequestMethod.POST)
    public String getUserList(String real_name){
		JsonReturn jr = new JsonReturn();
		try{
			//Map<>result
			List<TBUser> tbuserList=tbUserService.gettbUserList(real_name);
	        if(tbuserList!=null){
	        	jr.setObject(tbuserList);
	        	jr.setInfo("success");
	        }
		}catch(Exception e){
			jr.setInfo("false");
		}finally{
			return JSON.toJSONString(jr);
		}
    }
	
	@ResponseBody
	@RequestMapping(value="/delectUser",method=RequestMethod.POST)
	public JsonReturn delectUser(String login_name){
		JsonReturn jr = new JsonReturn();
		int b = tbUserService.delectUser(login_name);
		if(b!=0){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
		
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addUser",method=RequestMethod.POST)
	public JsonReturn addUser(TBUser tbuser){
		JsonReturn jr = new JsonReturn();
		try {
			tbuser.setId(BaseUtils.getUUID32());
			tbuser.setCreate_time(BaseUtils.getTime());
			tbuser.setPassword("123456");
			tbuser.setState("1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = tbUserService.addUser(tbuser);
		if(i==1){
			jr.setInfo("success");
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//获得某一用户的数据
	@ResponseBody
	@RequestMapping(value="/getUserByName",method=RequestMethod.POST)
	public JsonReturn getUserByName(String login_name){
		JsonReturn jr = new JsonReturn();
		TBUser tbUser = tbUserService.getUserByName(login_name);
		if(tbUser!=null){
			jr.setInfo("success");
			jr.setObject(tbUser);
		}else{
			jr.setInfo("false");
		}
		return jr;
	}
	
	//获得当前用户的数据
		@ResponseBody
		@RequestMapping(value="/getUser",method=RequestMethod.POST)
		public JsonReturn getUser(String login_name,Model model,HttpSession httpsession){
			JsonReturn jr = new JsonReturn();
			TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
			if(userSession!=null){
				jr.setInfo("success");
				jr.setObject(userSession);
			}else{
				jr.setInfo("false");
			}
			return jr;
		}
		
		//获得当前用户信息
		@ResponseBody
		@RequestMapping(value="/getuserdata",method=RequestMethod.POST)
		public JsonReturn getuserdata(Model model,HttpSession httpsession){
			JsonReturn jr = new JsonReturn();
			TBUser userSession = (TBUser) httpsession.getAttribute("tbuser");
			TBUser tbuser = tbUserService.getUserByName(userSession.getLogin_name());
			if(tbuser!=null){
				jr.setInfo("success");
				jr.setObject(tbuser);
			}else{
				jr.setInfo("false");
			}
			return jr;
		}
		
		@ResponseBody
		@RequestMapping(value="/updateUser",method=RequestMethod.POST)
		public JsonReturn updateUser(TBUser tbuser){
			JsonReturn jr = new JsonReturn();
			int i = tbUserService.updateUser(tbuser);
			if(i!=0){
				jr.setInfo("success");
			}else{
				jr.setInfo("flase");
			}
			return jr;
		}
}
